package com.hustzp.com.xichuangzhu.poetry.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.poetry.model.Works;

import java.util.List;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;

/**
 * Copyright (C) 2015 The huimai mobile client Project
 * All right reserved.
 *
 * @author: wuzhimin@huimai365.com
 * @date: 2016/5/31 17:12
 * @Description:
 */
public class PoetryAuthorAdapter extends BaseAdapter implements
        StickyListHeadersAdapter {

    private final Context mContext;
    private List<Works> mCountries;
    //    private int[] mSectionIndices;
//    private Character[] mSectionLetters;
    private LayoutInflater mInflater;

    public PoetryAuthorAdapter(Context context, List<Works> works) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        this.mCountries = works;
//        mSectionIndices = getSectionIndices();
//        mSectionLetters = getSectionLetters();
    }

//    private int[] getSectionIndices() {
//        ArrayList<Integer> sectionIndices = new ArrayList<Integer>();
//        char lastFirstChar = mCountries[0].charAt(0);
//        sectionIndices.add(0);
//        for (int i = 1; i < mCountries.length; i++) {
//            if (mCountries[i].charAt(0) != lastFirstChar) {
//                lastFirstChar = mCountries[i].charAt(0);
//                sectionIndices.add(i);
//            }
//        }
//        int[] sections = new int[sectionIndices.size()];
//        for (int i = 0; i < sectionIndices.size(); i++) {
//            sections[i] = sectionIndices.get(i);
//        }
//        return sections;
//    }
//
//    private Character[] getSectionLetters() {
//        Character[] letters = new Character[mSectionIndices.length];
//        for (int i = 0; i < mSectionIndices.length; i++) {
//            letters[i] = mCountries[mSectionIndices[i]].charAt(0);
//        }
//        return letters;
//    }

    @Override
    public int getCount() {
        return mCountries.size();
    }

    @Override
    public Object getItem(int position) {
        return mCountries.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.activity_author_item, parent, false);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Works works = mCountries.get(position);
        holder.tv_title.setText(works.getTitle());
        int endIndex = works.getContent().indexOf("。") + 1;
        int wEndIndex = works.getContent().indexOf("？") + 1;
        //遇到问号词,显示到问号位置
        if (wEndIndex != 0 && wEndIndex < endIndex) {
            endIndex = wEndIndex;
        }
        holder.tv_content.setText(works.getContent().substring(0, endIndex));
        return convertView;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        HeaderViewHolder holder;

        if (convertView == null) {
            holder = new HeaderViewHolder();
            convertView = holder.text = (TextView) mInflater.inflate(R.layout.activity_author_group, parent, false);
            convertView.setTag(holder);
        } else {
            holder = (HeaderViewHolder) convertView.getTag();
        }

        // set header text as first char in name
        Works works = mCountries.get(position);
        holder.text.setText(works.getKind_cn());

        return convertView;
    }

    /**
     * Remember that these have to be static, postion=1 should always return
     * the same Id that is.
     */
    @Override
    public long getHeaderId(int position) {
        Works works = mCountries.get(position);
        return works.getKind_cn().hashCode();
    }

    class HeaderViewHolder {
        TextView text;
    }

    class ViewHolder {
        TextView tv_title;
        TextView tv_content;
    }
}
